﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using HFW.Share;
using System.Windows.Forms;

namespace HFW.ADM
{
    public partial class frmPermissions : BaseForm
    {
        private string UserID = "";
        public frmPermissions()
        {
            InitializeComponent();
            multiCBUsers.Leave += new EventHandler(base.ComboLeaveEvent);
        }

        private void frmPermissions_Load(object sender, EventArgs e)
        {
            LoadMultiCombobox();
        }

        private void  LoadMultiCombobox()
        {
            using (var conn = new DataProvider.Connection())
            {
                try
                {
                    multiCBUsers.DataSource = conn.RunSQLQuery("ADM_UserAndGroup_lst", CommandType.StoredProcedure).Tables[0];
                }
                catch (Exception)
                {
                    MessageBoxEX.Show("Error 5 : Lỗi kết nối cơ sở dữ liệu", Applications.APPLICATION_TITLE_MESSAGEBOX,
                                      MessageBoxButtons.OK,
                                      MessageBoxIcon.Error);
                }
                
            }
        }

        private void multiCBUsers_ValueChanged(object sender, EventArgs e)
        {
            UserID = multiCBUsers.DropDownList.GetValue("UserID").ToString().Trim();
            if (UserID != "")
            {
                using (var conn = new DataProvider.Connection())
                {
                    try
                    {
                        var dstemp = new DataSet();
                        dstemp = conn.RunDynamicSQLQuery("ADM_UserPermissions_lst",
                                             new object[] { UserID });

                        grdOverview.DataSource = dstemp.Tables[0];
                    }
                    catch (Exception)
                    {
                        MessageBoxEX.Show("Error 6 : Lỗi kết nối cơ sở dữ liệu", Applications.APPLICATION_TITLE_MESSAGEBOX,
                                     MessageBoxButtons.OK,
                                     MessageBoxIcon.Error);
                    }
                   

                }
            }
            else
            {
                grdOverview.DataSource = null;
            }

           
        }

        private void grdOverview_CellValueChanged(object sender, Janus.Windows.GridEX.ColumnActionEventArgs e)
        {
            if (UserID!="")
            {
                if (e.Column.Key.Equals("IsPermiss"))
                {
                    using (var conn = new DataProvider.Connection())
                    {
                        try
                        {
                            conn.RunDynamicSQLNonQuery("ADM_UserPermissions_ups", new object[] { UserID, grdOverview.GetValue("Functionkey").ToString().Trim(), grdOverview.GetValue("IsPermiss") });
                        }
                        catch (Exception)
                        {
                            MessageBoxEX.Show("Error 7 : Lỗi kết nối cơ sở dữ liệu", Applications.APPLICATION_TITLE_MESSAGEBOX,
                                     MessageBoxButtons.OK,
                                     MessageBoxIcon.Error);
                        }
                        
                    }
                }
            }
            
        }
    }
}
